package com.pyrsana.faith

class UtilsTagLib {
    // Pyrsana utilities
    static namespace = "putils"
    
    /**
     * Renders given date with determined format.
     *
     * @attr date REQUIRED The date to display
     */
    def formatDate = { attrs ->
       out << attrs.date?.format("MMMM d, yyyy ' at ' h:mm:ss aaa")
    }
    
    /**
     * @attr value REQUIRED The string to truncate.
     * @attr maxlength The maximum number of characters to output before trailing with "...". Default 50.
     */
    def truncate = { attrs ->
        String value = attrs.value
        int maxlength = attrs.maxlength ? Long.parseLong(attrs.maxlength) : 50
        if (value.length() <= maxlength) {
            out << value
        }
        else {
            out << value.substring(0, maxlength) << '...'
        }
    }
}
